# Makefile for drbd.o
#
# This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
#
# drbd is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# drbd is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with drbd; see the file COPYING.  If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
#

VPATH = ../../drbd-headers:../shared

# variables set by configure
DISTRO = @DISTRO@
prefix = @prefix@
exec_prefix = @exec_prefix@
localstatedir = @localstatedir@
datarootdir = @datarootdir@
datadir = @datadir@
sbindir = @sbindir@
sysconfdir = @sysconfdir@
BASH_COMPLETION_SUFFIX = @BASH_COMPLETION_SUFFIX@
UDEV_RULE_SUFFIX = @UDEV_RULE_SUFFIX@
INITDIR = @INITDIR@
LIBDIR = @prefix@/lib/@PACKAGE_TARNAME@
CC = @CC@
CFLAGS = @CFLAGS@
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@
LN_S = @LN_S@

DRBD_LIB_DIR = @DRBD_LIB_DIR@
DRBD_RUN_DIR = @DRBD_RUN_DIR@
DRBD_LOCK_DIR = @DRBD_LOCK_DIR@
DRBD_CONFIG_DIR = @DRBD_CONFIG_DIR@
DRBD_BIN_DIR = @DRBD_BIN_DIR@

# features enabled or disabled by configure
WITH_83_SUPPORT = @WITH_83_SUPPORT@
WITH_84_SUPPORT = @WITH_84_SUPPORT@
WITH_UDEV = @WITH_UDEV@
WITH_XEN = @WITH_XEN@
WITH_PACEMAKER = @WITH_PACEMAKER@
WITH_RGMANAGER = @WITH_RGMANAGER@
WITH_BASHCOMPLETION = @WITH_BASHCOMPLETION@
WITH_WINDRBD = @WITH_WINDRBD@
WITH_CLITEST = @WITH_CLITEST@

# for some reason some of the commands below only work correctly in bash,
# and not in e.g. dash. I'm too lazy to fix it to be compatible.
SHELL=/bin/bash

# variables meant to be overridden from the make command line
ifeq ($(WITH_WINDRBD),yes)
DESTDIR ?=
else
DESTDIR ?= /
endif

CFLAGS += -Wall -I../../drbd-headers -I.. -I. -I../shared
ifeq ($(WITH_WINDRBD),yes)
CFLAGS += -I../windrbd-compat-headers -DWINDRBD
endif
CFLAGS += $(EXTRA_CFLAGS)

drbdadm-obj = drbdadm_scanner.o drbdadm_parser.o drbdadm_postparse.o \
	      drbdadm_main.o drbdadm_adjust.o drbdadm_dump.o drbdtool_common.o \
	      drbdadm_usage_cnt.o drbd_buildtag.o registry.o config_flags.o \
	      libnla.o drbd_nla.o shared_tool.o shared_main.o shared_parser.o

drbdsetup-core-obj = libnla.o registry.o drbdsetup.o drbdtool_common.o \
		     drbd_buildtag.o drbd_strings.o config_flags.o drbd_nla.o \
		     wrap_printf.o drbdsetup_colors.o shared_tool.o \
		     drbdsetup_events2.o

drbdmeta-obj = drbdmeta.o drbdmeta_scanner.o drbdtool_common.o drbd_buildtag.o \
	       drbd_strings.o shared_tool.o

ifeq ($(WITH_WINDRBD),yes)
drbdadm-obj += drbdadm_windrbd.o shared_windrbd.o libgenl_windrbd.o drbdtool_common_windrbd.o path_windrbd.o
drbdsetup-core-obj += shared_windrbd.o libgenl_windrbd.o drbdtool_common_windrbd.o drbdsetup_windrbd.o path_windrbd.o
drbdmeta-obj += shared_windrbd.o drbdmeta_windrbd.o drbdtool_common_windrbd.o path_windrbd.o
else
drbdadm-obj += libgenl.o drbdadm_linux.o drbdtool_common_linux.o path_linux.o shared_linux.o
drbdsetup-core-obj += libgenl.o drbdtool_common_linux.o drbdsetup_linux.o path_linux.o shared_linux.o
drbdmeta-obj += drbdmeta_linux.o drbdtool_common_linux.o path_linux.o shared_linux.o
endif

drbdsetup-obj = $(drbdsetup-core-obj) drbdsetup_main.o

drbdsetup-instrumented-test-obj = $(drbdsetup-core-obj) test/drbdsetup_events2_instrumented.o

all-obj := $(drbdadm-obj) $(drbdsetup-obj) $(drbdmeta-obj) test/drbdsetup_events2_instrumented.o

all: tools

../shared_prereqs.mk: ;
include ../shared_prereqs.mk

tools: drbdadm drbdmeta drbdsetup

drbdadm: $(drbdadm-obj)
	$(LINK.c) $(LDFLAGS) -o $@ $^ $(LIBS)

drbdadm_scanner.c: drbdadm_scanner.fl drbdadm_parser.h
	flex -s -odrbdadm_scanner.c drbdadm_scanner.fl

drbdsetup: $(drbdsetup-obj)
	$(LINK.c) $(LDFLAGS) -o $@ $^ $(LIBS)

drbdmeta: $(drbdmeta-obj)
	$(LINK.c) $(LDFLAGS) -o $@ $^ $(LIBS)

test/drbdsetup_events2_instrumented: $(drbdsetup-instrumented-test-obj)
	$(LINK.c) $(LDFLAGS) -o $@ $^ $(LIBS)

.PHONY: test
ifeq ($(WITH_CLITEST),yes)
test: drbdadm test/drbdsetup_events2_instrumented
	cd ../../tests/v9 && ln -sf ../../user/v9/drbdadm . && ln -sf ../../user/v9/test/drbdsetup_events2_instrumented .
	../../tests/run.sh ../../tests/v9/*.test
else
test:
endif

clean:
	rm -f drbdadm_scanner.c
	rm -f drbdsetup drbdadm drbdmeta test/drbdsetup_events2_instrumented $(all-obj)
	rm -f drbd_strings.c drbd_strings.h
	rm -f *~

distclean: clean
	rm -f $(all-dep)

install:
	install -m 0755 -d $(DESTDIR)$(sbindir)
	install -m 0755 -d $(DESTDIR)$(localstatedir)/lib/drbd
	install -m 0755 -d $(DESTDIR)$(localstatedir)/run/drbd
	install -m 0700 -d $(DESTDIR)$(localstatedir)/run/drbd/lock
	if getent group haclient > /dev/null 2> /dev/null ; then	\
		install -g haclient -m 4750 drbdsetup $(DESTDIR)$(sbindir) ;	\
		install -g haclient -m 4750 drbdmeta $(DESTDIR)$(sbindir) ;	\
		install -m 755 drbdadm $(DESTDIR)$(sbindir) ;		\
	else								\
		install -m 755 drbdsetup $(DESTDIR)$(sbindir) ;		\
		install -m 755 drbdmeta $(DESTDIR)$(sbindir) ;		\
		install -m 755 drbdadm $(DESTDIR)$(sbindir) ;		\
	fi
	if test -d $(DESTDIR)/sbin && \
	 ! test $(DESTDIR)/sbin -ef $(DESTDIR)$(sbindir) ; then		\
		ln -sf $(sbindir)/drbdsetup $(DESTDIR)/sbin ;		\
		ln -sf $(sbindir)/drbdmeta $(DESTDIR)/sbin ;		\
		ln -sf $(sbindir)/drbdadm $(DESTDIR)/sbin ;		\
	fi
	install -d $(DESTDIR)$(DRBD_LIB_DIR)

uninstall:
	rm -f $(DESTDIR)$(sbindir)/{drbdsetup,drbdadm,drbdmeta}
	rm -f $(DESTDIR)/sbin/{drbdsetup,drbdadm,drbdmeta}

spell:
	for f in drbdadm_adjust.c drbdadm_main.c drbdadm_parser.c drbdadm_usage_cnt.c drbdmeta.c drbdsetup.c drbdtool_common.c; do \
	 	aspell --save-repl --dont-backup --personal=./../documentation/aspell.en.per check $$f; \
	done

.PHONY: install uninstall clean distclean spell
../../configure:
	@echo "please (re-)run ./autogen.sh with appropriate arguments"; exit 1
../../config.status: ../../configure
	@echo "please (re-)run ./configure with appropriate arguments"; exit 1
Makefile.in: ;
Makefile: Makefile.in ../../config.status
	cd ../.. && ./config.status user/v9/Makefile

